package com.zegome.game.oanquan.scene.play;

import java.util.ArrayList;

import com.badlogic.gdx.math.Vector2;
import com.zegome.game.entity.sprite.Sprite;
import com.zegome.game.entity.text.Text;
import com.zegome.game.oanquan.GameCenter;

/**
 * (C) 2013 ZeroTeam
 * @author QuanLT
 */

public class ScorePond {
	// ===========================================================
	// Constants
	// ===========================================================

	// ===========================================================
	// Fields
	// ===========================================================
	private ArrayList<Sprite> mStoneSprites;
	
	public final Vector2 mPosition;
	public Text mText;

	private int mCourting;
	private int mTimeSurPlus;
	private final int mFlag;
	private int mScore;
	
	// ===========================================================
	// Constructor
	// ===========================================================
	public ScorePond(final float x, final float y, final int flag) {
		mPosition = new Vector2(x, y);
		mFlag = flag;
		
	}
	
	// ===========================================================
	// Getter & Setter
	// ===========================================================
	public void setText(final Text text) {
		mText = text;
		mText.setPosition(mPosition.x + 50, mPosition.y + 25);
	}
	
	public int getFlag() {
		return mFlag;
	}
	
	public int getScore() {
		return mScore;
	}
	
	public int getScore(final int level) {
		int sum = 0;
		
		sum += (mScore + mCourting * 10) * level + (int)Math.min(mTimeSurPlus * 0.1f, 10);
		
		return sum;
	}

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	// ===========================================================
	// Methods
	// ===========================================================
	public void addPosition(final Sprite stone) {
		final float x = GameCenter.GAME_RANDOM.nextInt(50) + mPosition.x + 15;
		final float y = GameCenter.GAME_RANDOM.nextInt(50) + mPosition.y + 20;
		stone.setPosition(x, y);
	}
	
	public void attachStone(final Sprite stone) {
		if (mStoneSprites == null) {
			mStoneSprites = new ArrayList<Sprite>();
		}
		mStoneSprites.add(stone);
		mScore++;
		mText.setText(""+mScore);
	}
	
	public void attachStone(final Sprite...sprites) {
		for (int i = 0; i < sprites.length; i++) {
			attachStone(sprites[i]);
		}
	}
	
	public Sprite removeStone() {
		if (mStoneSprites == null) {
			throw new IllegalArgumentException("mStoneSprites must be different from null");
		}
		if (mStoneSprites.size() > 0) {
			mScore--;
			mText.setText(""+mScore);
			return mStoneSprites.remove(0);
		}
		return null;
	}
	
	public void addTimeSurPlus(final int time) {
		mTimeSurPlus += time;
	}
	
	public void eatCourting() {
		mCourting++;
	}
	
	public void clear() {
		if (mStoneSprites == null) {
			return;
		}
		mStoneSprites.clear();
		mScore = 0;
		mCourting = 0;
		mTimeSurPlus = 0;
		mText.setText(""+mScore);
	}
	
	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
